<template>
    <div id="">
        <!--登录页面-->
        <div class="ff">
            <div><span>用户名：</span><input type="text" placeholder="请输入用户名" v-model="name"></div>
            <div><span>密码：</span><input @blur="blur" type="password" placeholder="请输入密码" v-model="pass"></div>
        </div>

        <div class="link">
            <!--<router-link >&lt;!&ndash;v-bind="{to:denlu}"to="/index"&ndash;&gt;-->
                <button @click="delu">登录</button>
            <!--</router-link>-->
        </div>

    </div>
</template>

<script>
    import { Toast } from 'mint-ui';
	export default {
		name: "",
		data() {
			return {
                list:{},
			    name:"",/*zhangsan*/
                pass:"",/*123456*/
                denlu:'',
                re:''
            }
		},
        created(){

        },
		methods:  {
            blur:function(){
                this.denlu = '';
                let param= new URLSearchParams();//为发送的数据转换格式
                param.append("username",this.name);
                param.append("userpass",this.pass);
                this.$ajax({
                    method:'post',
                    url:'http://134.175.237.162/index.php/index/login',
                    data:param
                }).then((res)=>{
                    if(res.data.code==1){
                        this.re = res.data.code;
                        this.list = res.data.data;
                        console.log(this.list,res);
                        //setTimeout(function () {
                            this.denlu="/index";
                            this.$router.push(this.denlu)
                        //},0)

                        //console.log(this.denlu)
                        //储存用户数据在浏览器
                        var ses = window.sessionStorage;
                        var d = JSON.stringify(this.list);
                        //把拿到的数据放到sessionStorage中
                        ses.setItem('data', d);
                        // console.log(JSON.parse(sessionStorage.getItem("data")))
                    }else{
                        console.log('请求异常');
                        //console.log(res.data.msg)//登录失败
                    }
                    //console.log(res.data)
                }).catch(()=>{
                    console.log('网络异常')
                })
            },
            delu:function () {
                this.denlu = '';
                let param= new URLSearchParams();//为发送的数据转换格式
                param.append("username",this.name);
                param.append("userpass",this.pass);
                this.$ajax({
                    method:'post',
                    url:'http://134.175.237.162/index.php/index/login',
                    data:param
                }).then((res)=>{
                    if(res.data.code==1){
                        this.re = res.data.code;
                        this.list = res.data.data;

                        //this.$router.push({path:"/index"});
                        this.$router.push('/index');

                        //console.log(this.denlu)
                        //储存用户数据在浏览器
                        var ses = window.sessionStorage;
                        var d = JSON.stringify(this.list);
                        //把拿到的数据放到sessionStorage中
                        ses.setItem('data', d);
                        // console.log(JSON.parse(sessionStorage.getItem("data")))
                    }else{
                        console.log('请求异常');
                        //console.log(res.data.msg)//登录失败
                    }
                    //console.log(res.data)
                }).catch(()=>{
                    console.log('网络异常')
                })
               // this.blur();
                if(this.name.length==0){
                    Toast({
                        message: '账号不能为空！',
                        position: 'bottom',
                        duration: 5000
                    });
                }else if(this.pass.length==0){
                    Toast({
                        message: '密码不能为空！',
                        position: 'bottom',
                        duration: 5000
                    });
                }

                if(this.re == 1){
                    console.log(this.re)
                }
            }
		}
	}
</script>

<style scoped>
    .ff{
        background-color: white;
        width: 230px;
        margin: 150px auto 0;
        border-radius: 6px;
        padding:20px 10px;
    }
    .ff>div{display: flex;justify-content: center;margin-bottom: 10px}
    .ff>div span{width: 32%;}
    .link{text-align: center;margin-top: 30px}
    .link button{width: 50px}
    input[type='text'],input[type='password']{
        width:68%;
        font-size: 16px;
        height: 25px;
        margin-bottom: 0;
        padding: 10px 15px;
    }
</style>
